package com.base.common.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.base.common.db.constant.ConstantPushMessage;
import com.base.common.db.constant.ConstantUrlDataCache;
import com.base.common.db.constant.ConstantUserInfoTable;
import com.base.common.util.StringUtils;
import com.base.common.util.log.LogUtil;

public class DBHelper extends SQLiteOpenHelper {
	private static final String TAG = "DBHelper";

	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	private static DBHelper mInstance = null;

	public static DBHelper getInstance(Context ctx) {

		if (mInstance == null) {
			mInstance = new DBHelper(ctx.getApplicationContext());
		}
		return mInstance;
	}

	public DBHelper(Context context) {
		this(context, DBConstants.ShopDB.DATABASE_NAME, null,
				DBConstants.ShopDB.DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		LogUtil.i(TAG,
				"DBHelper , the database is created for the first time. and start create table ...");
		try {
			/**
			 * 用户表
			 */
			db.execSQL("CREATE TABLE IF NOT EXISTS "
					+ ConstantUserInfoTable.TABLENAME
					+ "(_id INTEGER primary key autoincrement ,"
					+ ConstantUserInfoTable.USERID + " TEXT ,"
					+ ConstantUserInfoTable.PARTYID + " TEXT ,"
					+ ConstantUserInfoTable.ACCOUNT + " TEXT ,"
					+ ConstantUserInfoTable.EMAIL + " TEXT ,"
					+ ConstantUserInfoTable.PROFILE + " TEXT ,"
					+ ConstantUserInfoTable.MOBILE + " TEXT ,"
					+ ConstantUserInfoTable.PARTYNAME + " TEXT ,"
					+ ConstantUserInfoTable.CARTID + " TEXT ,"
					+ ConstantUserInfoTable.LOGINTIME + " TEXT ,"
					+ ConstantUserInfoTable.UNIONID + " TEXT ,"
					+ ConstantUserInfoTable.LOGINTYPE + " TEXT ,"
					+ ConstantUserInfoTable.PARTYDESC + " TEXT  )");
			
			/**
			 * 网络数据缓存
			 */
			db.execSQL("CREATE TABLE IF NOT EXISTS "
					+ ConstantUrlDataCache.TABLENAME
					+ "(_id INTEGER primary key autoincrement ,"
					+ ConstantUrlDataCache.ID + " TEXT,"
					+ ConstantUrlDataCache.DATA + " TEXT ,"
					+ ConstantUrlDataCache.URL + " TEXT ,"
					+ ConstantUrlDataCache.TIME + " LONG )");
			
			/**
			 * 推送消息
			 */
			db.execSQL("CREATE TABLE IF NOT EXISTS "
					+ ConstantPushMessage.TABLENAME
					+ "(_id INTEGER primary key autoincrement ,"
					+ ConstantPushMessage.ID + " TEXT,"
					+ ConstantPushMessage.MSGCONTENT + " TEXT ,"
					+ ConstantPushMessage.MSGSHOWTIME + " TEXT ,"
					+ ConstantPushMessage.MSGTARGETCONTENT + " TEXT ,"
					+ ConstantPushMessage.MSGTARGETID + " TEXT ,"
					+ ConstantPushMessage.MSGTARGETTYPE + " TEXT ,"
					+ ConstantPushMessage.MSGTITLE + " TEXT ,"
					+ ConstantPushMessage.PUSHSERVICE + " TEXT ,"
					+ ConstantPushMessage.STATUS + " INTEGER ,"
					+ ConstantPushMessage.DEADLINE + " LONG )");
			
			
		} catch (Exception e) {
			LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		// 如果更新的新版本和老版本一致 强制退出
		LogUtil.e(TAG, "Upgrading database from version " + oldVersion
				+ " to " + newVersion + "...");
		// 更新时在此处修改表结构
		switch (newVersion) {
		
		}
	}

			

}
